package com.xujiahao.controller;

import com.xujiahao.common.R;
import com.xujiahao.util.incident.KWICSubject;
import com.xujiahao.util.incident.Alphabetizer;
import com.xujiahao.util.incident.Input;
import com.xujiahao.util.incident.Output;
import com.xujiahao.util.incident.Shift;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class incidentController {

    @GetMapping("/incident")
    public R<List<String>> incident(){
        //创建主题
        KWICSubject kwicSubject = new KWICSubject();
        //创建观察者
        Input input = new Input("E:\\input.txt");
        Shift shift = new Shift(input.getLineTxt());
        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());
        Output output = new Output(alphabetizer.getKwicList(), "E:\\output.txt");

        // 将观察者加入主题
        kwicSubject.addObserver(input);
        kwicSubject.addObserver(shift);
        kwicSubject.addObserver(alphabetizer);
        kwicSubject.addObserver(output);
        // 逐步调用各个观察者
        kwicSubject.startKWIC();
        return R.success(output.kwicList);
    }
}
